package com.easy;

/**
 * Created by Lxk on 2019/5/17.
 */
public class Solution541 {

    public String reverseStr(String s, int k) {
        if(s==null){
            return null;
        }
        int page = s.length()/k + ((s.length()%k==0) ? 0 : 1);
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i < page; i++){
            if(i%2==0){
                //转置
                int pos = (i+1)*k-1 > s.length()-1 ? s.length()-1 : (i+1)*k-1;
                for(; pos >= i*k ; pos--){
                    sb.append(s.charAt(pos));
                }
            }else{
                //直接复制
                int end = (i+1)*k-1 > s.length()-1 ? s.length()-1 : (i+1)*k-1;
                for(int pos = (i*k); pos <= end; pos++){
                    sb.append(s.charAt(pos));
                }
            }
        }
        return sb.toString();
    }

}
